{{!

  Copyright (c) Meta Platforms, Inc. and affiliates.

  Licensed under the Apache License, Version 2.0 (the "License");
  you may not use this file except in compliance with the License.
  You may obtain a copy of the License at

      http://www.apache.org/licenses/LICENSE-2.0

  Unless required by applicable law or agreed to in writing, software
  distributed under the License is distributed on an "AS IS" BASIS,
  WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
  See the License for the specific language governing permissions and
  limitations under the License.

}}{{!

This is a template to share the container __make_instance .pyx implementation
between types.pyx (default mode) and converters.pyx (auto-migrate mode)


}}
    cdef {{> types/cython_cpp_type}} c_inst
    {{#type:set?}}{{#type:set_elem_type}}
    cdef {{> types/cython_cpp_type}} c_item
    {{/type:set_elem_type}}{{/type:set?}}
    {{#type:map?}}{{#type:key_type}}
    cdef {{> types/cython_cpp_type}} c_key
    {{/type:key_type}}{{/type:map?}}
    if items is None:
        return cmove(c_inst)
{{^type:map?}}
    {{#type:containerOfString?}}
    if isinstance(items, str):
        raise TypeError("If you really want to pass a string into a {{> types/pep484_type}} field, explicitly convert it first.")
    {{/type:containerOfString?}}
    for item in items:
{{/type:map?}}
{{#type:list?}}
        {{#type:list_elem_type}}
        {{#type:container?}}
        if item is None:
            raise TypeError("None is not of the type {{> types/pep484_type}}")
        if not isinstance(item, {{> types/python_type}}):
            item = {{> types/python_type}}(item)
        {{/type:container?}}
        {{^type:container?}}
        if not isinstance(item, {{> types/python_is_instance_type}}):
            raise TypeError(f"{item!r} is not of type {{> types/pep484_type}}")
        {{#type:integer?}}
        {{! inject cython int overflow checks }}
        item = <{{> types/cython_python_type}}> item
        {{/type:integer?}}
        {{/type:container?}}
        c_inst.push_back({{> types/cython_python_to_cpp_item}})
        {{/type:list_elem_type}}
{{/type:list?}}{{#type:set?}}
        {{#type:set_elem_type}}
        {{#type:container?}}
        if item is None:
            raise TypeError("None is not of type {{> types/pep484_type}}")
        if not isinstance(item, {{> types/python_type}}):
            item = {{> types/python_type}}(item)
        {{/type:container?}}
        {{^type:container?}}
        if not isinstance(item, {{> types/python_is_instance_type}}):
            raise TypeError(f"{item!r} is not of type {{> types/pep484_type}}")
        {{#type:integer?}}
        {{! inject cython int overflow checks }}
        c_item = <{{> types/cython_python_type}}> item
        {{/type:integer?}}
        {{/type:container?}}
        {{^type:integer?}}
        c_item = {{> types/cython_python_to_cpp_item}}
        {{/type:integer?}}
        c_inst.insert(c_item)
        {{/type:set_elem_type}}
{{/type:set?}}
{{#type:map?}}
    for key, item in items.items():
        {{#type:key_type}}
        {{#type:container?}}
        if key is None:
            raise TypeError("None is not of type {{> types/pep484_type}}")
        if not isinstance(key, {{> types/python_type}}):
            key = {{> types/python_type}}(key)
        {{/type:container?}}
        {{^type:container?}}
        if not isinstance(key, {{> types/python_is_instance_type}}):
            raise TypeError(f"{key!r} is not of type {{> types/pep484_type}}")
        {{#type:integer?}}
        {{! inject cython int overflow checks }}
        c_key = <{{> types/cython_python_type}}> key
        {{/type:integer?}}
        {{/type:container?}}
        {{^type:integer?}}
        c_key = {{> types/cython_python_to_cpp_key}}
        {{/type:integer?}}
        {{/type:key_type}}
        {{#type:value_type}}
        {{#type:container?}}
        if item is None:
            raise TypeError("None is not of type {{> types/pep484_type}}")
        if not isinstance(item, {{> types/python_type}}):
            item = {{> types/python_type}}(item)
        {{/type:container?}}
        {{^type:container?}}
        if not isinstance(item, {{> types/python_is_instance_type}}):
            raise TypeError(f"{item!r} is not of type {{> types/pep484_type}}")
        {{#type:integer?}}{{!
        }}{{! inject cython int overflow checks }}
        item = <{{> types/cython_python_type}}> item
        {{/type:integer?}}
        {{/type:container?}}
        {{/type:value_type}}

        c_inst[c_key] = {{#type:value_type}}{{!
            }}{{> types/cython_python_to_cpp_item}}{{/type:value_type}}
{{/type:map?}}
    return cmove(c_inst)
